package up7.database.sql;
import java.sql.*;
import java.text.ParseException;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import up7.biz.DbFolder;
import up7.biz.DbFolderMySQL;
import up7.biz.DbFolderOracle;
import up7.database.DbHelper;
import up7.model.FileInf;
import up7.sql.DataBaseType;
import up7.sql.SqlExec;
import up7.sql.SqlParam;
import up7.sql.SqlTable;
import up7.utils.ConfigReader;

/*
 * 原型
*/
public class SqlFolder {

	public static SqlFolder build(){
		DataBaseType dt = ConfigReader.dbType();
		/*
		if(dt == DataBaseType.Oracle||
				dt == DataBaseType.KingBase||
				dt == DataBaseType.DMDB) return new DbFolderOracle();
		else if(dt == DataBaseType.MySQL) return new DbFolderMySQL();
		*/
		return new SqlFolder();
	}

	public SqlFolder()
	{
	}

	public SqlTable table(){
		return SqlTable.build("up7_folders");
	}

	public void remove(String idSign,String uid)
	{
		String sql = "{call fd_remove(?,?)}";
		DbHelper db = new DbHelper();
		try 
		{
			CallableStatement cmd = db.GetCommandStored(sql);
			cmd.setString(1, idSign);
			cmd.setString(2, uid);
			cmd.execute();
			cmd.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	static public int count(String id)
	{
		String sql = String.format("select count(*) from up7_files where f_pidRoot='%s'", id);
		
		DbHelper db = new DbHelper();
		int count = db.ExecuteScalar(sql);
		return count;
	}
	
	public void add(FileInf f)
	{	
		StringBuilder sb = new StringBuilder();
		sb.append("insert into up7_folders(");
		sb.append(" f_id");
		sb.append(",f_uid");
		sb.append(",f_nameLoc");
		sb.append(",f_pathLoc");
		sb.append(",f_pathSvr");
		sb.append(",f_pathRel");
		sb.append(",f_fileCount");
		sb.append(",f_pid");
		
		sb.append(") values (");
				
		sb.append(" ?");
		sb.append(",?");
		sb.append(",?");
		sb.append(",?");
		sb.append(",?");
		sb.append(",?");
		sb.append(",?");
		sb.append(",?");
		sb.append(")");

		DbHelper db = new DbHelper();
		PreparedStatement cmd = db.GetCommand(sb.toString());
		try {
			cmd.setString(1, f.id);
			cmd.setString(2, f.uid);
			cmd.setString(3, f.nameLoc);
			cmd.setString(4, f.pathLoc);
			cmd.setString(5, f.pathSvr);
			cmd.setString(6, f.pathRel);
			cmd.setInt(7, f.fileCount);
			cmd.setString(8, f.pid);
		} catch (SQLException e) {e.printStackTrace();}
		
		db.ExecuteNonQuery(cmd);		
	}

	//批量添加
	public void addBatch(List<FileInf> arr) throws SQLException, ParseException, IllegalAccessException {
		this.table().inserts(arr);
	}
}